<?php


namespace App\Http\Controllers\Admin;


use App\Common\CommonHelpers;
use App\Http\Controllers\Controller;
use App\Models\RsReport;
use App\Models\RsReportAuth;
use App\Models\RsUsers;
use App\Models\User;
use App\Models\View\RsVUser;
use Illuminate\Http\Request;

class ReportController extends Controller{

    //报表视图
    public function index(){
       return view('admin.report.index');
    }

    //查询报表数据
    public function apiList(Request $request){
        //查询消息数据
        $data=RsReport::lists($request->input('page', 1), $request->input('limit', 20), $request->input());
        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);
    }


    //添加报表
    public function create(Request $request){
        $model=[];
        if($id=$request->input("id",false)){
           $model=RsReport::where(['id'=>$id])->first();
        }
        return view('admin.report.create',['model'=>$model,'auth'=>[]]);
    }


    //删除报表数据
    public function delete(Request $request){
        $id=$request->input("id");
        try {
            //删除权限人员
            RsReportAuth::where(['report_id'=>$id])->delete();
            //删除报表
            RsReport::where(['id'=>$id])->delete();

            return CommonHelpers::responseFormat(0,'','删除成功');

        }catch (\Exception $exception){
            return CommonHelpers::responseFormat(1,'','删除失败');

        }
    }


    //对报表进行添加数据
    public function createApi(Request $request){
        $data=[];
        $data['report_name']=$request->input("report_name");
        $data['id']=$request->input("id",false);
        $data['is_show_home']=$request->input("is_show_home");
        $res=RsReport::created($data);
        if($res){
            return CommonHelpers::responseFormat(0,'','添加成功');

        }else{
            return CommonHelpers::responseFormat(0,'','添加失败');

        }
    }

    //添加权限
    public function auth(Request $request){
        $id=$request->input("id",false);

        $data=RsUsers::lists();
        if($id){
            $model=RsReportAuth::query()->where(['id'=>$id])->first()->toArray();

        }else{
            $model=[];
        }
        return view('admin.report.selectlist',['data'=>$data['data'],'model'=>$model]);
    }

    //对权限人员进行添加
    public function userApiCreate(Request $request){
        $data=[];
        $ex=explode("_",$request->input('name'));
        $data['user_id']=$ex[0];
        $data['remark']=$request->input('remark');
        $data['id']=$request->input("id",false);
        $res=RsReportAuth::creates($data);
        if($res){
            return CommonHelpers::responseFormat(0,'','添加成功');

        }else{
            return CommonHelpers::responseFormat(0,'','添加失败');

        }
    }

    //查询有权限的人员
    public function userList(Request $request){
        $report_id=$request->input("report_id",'');
        $data=RsReportAuth::with('getRsUser','getUser')->where(['report_id'=>$report_id])->orWhere(['report_id'=>null])->get()->toArray();
        $count=count($data);
        $data=compact('count', 'data');

        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);

    }


    //删除无用的数据
    public function userDel(){
        $data=RsReportAuth::where(['report_id'=>null])->orWhere(['report_id'=>''])->delete();
        return CommonHelpers::responseFormat(0,'','');

    }

    //删除某天权限人员
    public function authDelete(Request $request){
        $id=$request->input("id");
        $data=RsReportAuth::where(['id'=>$id])->delete();
        if($data){
            return CommonHelpers::responseFormat(0,'','删除成功');

        }else{
            return CommonHelpers::responseFormat(1,'','删除失败');

        }

    }


    //查询权限
//    public function selectList(Request $request){
//        $data=RsUsers::lists($request->input('page', 1), $request->input('limit', 20), $request->input());
//        return response($data ? array_merge(['code' => 0, 'msg' => '查询成功'],  $data) : ['code' => 906, 'msg' => '当前没有更多数据']);
//
//
//    }



}
